package com.fafa.pos.dao.impl;

import android.content.ContentValues;
import android.database.Cursor;
import com.fafa.pos.dao.DBUtil;
import com.fafa.pos.dao.IDateLimitRuleInfoDao;
import com.fafa.pos.pojo.CardInfo;
import com.fafa.pos.pojo.DateLimitRuleInfo;
import com.fafa.pos.pojo.VipGroupInfo;
import com.fafa.pos.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhouBing on 2017/8/10.
 *
 * SQLite -- T_DATELIMITRULEINFO
 */
public class DateLimitRuleInfoDaoImpl implements IDateLimitRuleInfoDao{

    private DBUtil dbUtil;
    private ConfigDaoImpl configDao;

    public DateLimitRuleInfoDaoImpl(DBUtil dbUtil) {
        this.dbUtil = dbUtil;
        configDao = new ConfigDaoImpl(dbUtil);
    }

    @Override
    public void saveDateLimitRuleInfoList(List<DateLimitRuleInfo> dateLimitRuleInfos) {

        dbUtil.getDB().beginTransaction();

        try{
            for (DateLimitRuleInfo dateLimitRuleInfo:dateLimitRuleInfos) {

                //Id存在则更新不存在新增
                String sql = "select LIMIT_ID from T_DATELIMITRULEINFO where LIMIT_ID = ?";
                Cursor cursor = dbUtil.getDB().rawQuery(sql,new String[]{dateLimitRuleInfo.getLimitId()});

                ContentValues contentValues = new ContentValues();
                contentValues.put("LIMIT_ID",dateLimitRuleInfo.getLimitId());
                contentValues.put("LIMIT_CODE",dateLimitRuleInfo.getLimitCode());
                contentValues.put("LIMIT_NAME",dateLimitRuleInfo.getLimitName());
                contentValues.put("LIMIT_TYPE",dateLimitRuleInfo.getLimitType());
                contentValues.put("ACCT_TYPE_ID",dateLimitRuleInfo.getAcctTypeId());
                contentValues.put("GROUP_ID",dateLimitRuleInfo.getGroupId());
                contentValues.put("LINE_ID",dateLimitRuleInfo.getLineId());
                contentValues.put("PERIOD_ID",dateLimitRuleInfo.getPeriodId());
                contentValues.put("LIMIT_AMT",dateLimitRuleInfo.getLimitAmt());
                contentValues.put("LIMIT_NUM",dateLimitRuleInfo.getLimitNum());
                contentValues.put("STATUS",dateLimitRuleInfo.getStatus());
                contentValues.put("VISIBLE",dateLimitRuleInfo.getVisible());
                contentValues.put("UPDATE_DATE",dateLimitRuleInfo.getUpdate());

                if(cursor.moveToNext()){
                    dbUtil.getDB().update("T_DATELIMITRULEINFO",contentValues," LIMIT_ID = ?",new String[]{dateLimitRuleInfo.getLimitId()});
                }else{
                    dbUtil.getDB().insert("T_DATELIMITRULEINFO",null,contentValues);
                }
                if(cursor!=null){
                    cursor.close();
                }
            }
            configDao.saveItem("T_DATELIMITRULEINFO_UPDATE_DATE", DateUtil.getCurDateStr());
            dbUtil.getDB().setTransactionSuccessful();
        }finally {
            dbUtil.getDB().endTransaction();
            dbUtil.close();
        }
    }

    @Override
    public List<DateLimitRuleInfo> getDateLimitRuleInfo(String groupId) {

        List<DateLimitRuleInfo> dateLimitRuleInfos = new ArrayList<DateLimitRuleInfo>();

        String sql = "select * from T_DATELIMITRULEINFO where GROUP_ID = ?";
        Cursor cursor = dbUtil.getDB().rawQuery(sql,new String[]{groupId});
        while(cursor.moveToNext()){
            DateLimitRuleInfo dateLimitRuleInfo = new DateLimitRuleInfo();

            dateLimitRuleInfo.setLimitId(cursor.getString(0));
            dateLimitRuleInfo.setLimitCode(cursor.getString(1));
            dateLimitRuleInfo.setLimitName(cursor.getString(2));
            dateLimitRuleInfo.setLimitType(cursor.getString(3));
            dateLimitRuleInfo.setAcctTypeId(cursor.getString(4));
            dateLimitRuleInfo.setGroupId(cursor.getString(5));
            dateLimitRuleInfo.setLineId(cursor.getString(6));
            dateLimitRuleInfo.setPeriodId(cursor.getString(7));
            dateLimitRuleInfo.setLimitAmt(cursor.getString(8));
            dateLimitRuleInfo.setLimitNum(cursor.getString(9));
            dateLimitRuleInfo.setStatus(cursor.getString(10));
            dateLimitRuleInfo.setVisible(cursor.getString(11));
            dateLimitRuleInfo.setUpdate(cursor.getString(12));

            dateLimitRuleInfos.add(dateLimitRuleInfo);
        }
        if(cursor!=null){
            cursor.close();
        }
        dbUtil.close();
        return dateLimitRuleInfos;
    }
}
